import requests
import re
from selenium import webdriver
from lxml import etree
import time

def chromesite(useragent = "",ip = "",headless = True,imge = False,linuxenv = False):
    chrome_options = webdriver.ChromeOptions()

    if useragent == False:
        chrome_options.add_argument(
            "user-agent:'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'")
    else:
        chrome_options.add_argument("user-agent:"+useragent)

    if headless == True:
        chrome_options.add_argument('--headless')

    if imge == False:
        prefs = {'profile.default_content_setting_values': {'images': 2}}
        chrome_options.add_experimental_option('prefs', prefs)
    if ip:
        chrome_options.add_argument('--proxy-server='+ip)

    chrome_options.add_argument('--no-sandbox')
    if linuxenv == True:
        return webdriver.Chrome("./chromedriver",options=chrome_options)

    return webdriver.Chrome(options=chrome_options)



def openIpihuan(pagenumber):
    driverIp = chromesite()
    driverIp.get("https://ip.ihuan.me/"+pagenumber)
    time.sleep(2)
    html = driverIp.page_source
    etr=etree.HTML(html)
    pageip=etr.xpath("//tr/td[1]/a/text() | //tr/td[2]/text()")
    pagenumber = etr.xpath("//ul[@class='pagination']/li[8]/a/@href")[0]
    iplist = []
    for i in range(0,len(pageip),2):
        ipdict = {}
        ipdict["http"] = "http://"+pageip[i]+":"+pageip[i+1]
        iplist.append(ipdict)

    driverIp.quit()
    return pagenumber,iplist


#No.003-2
#验证ip可用性
#参数：
#url：目标网站
#ip：被验证ip 格式{'httpX': 'httpX://ip:port'}

#返回：若IP可用则返回，不可以不返回

# headers应随目标网站变化而变化，

def trydaili(url,ip):
    try:
        headers = {
            'authority': 'api.hkgolden.com',
            'method': 'GET',
            'path': '/v1/live',
            'scheme': 'https',
            'accept': 'application/json, text/plain, */*',
            'accept-encoding': 'gzip, deflate, br',
            'accept-language': 'zh-CN,zh;q=0.9',
            'hkgauth': 'null',
            'origin': 'https://forum.hkgolden.com',
            'referer': 'https://forum.hkgolden.com/',
            'sec-ch-ua': '"Chromium";v="92", " Not A;Brand";v="99", "Google Chrome";v="92"',
            'sec-ch-ua-mobile': '?0',
            'sec-fetch-dest': 'empty',
            'sec-fetch-mode': 'cors',
            'sec-fetch-site': 'same-site',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36',
        }
        r = requests.get(url=url,headers = headers,proxies=ip,timeout = 15)
        if r.status_code == 200:
            return ip
    except Exception as e:
        print(e)

#No.003-3
#使用调用 openIpihuan() 和trydaili()
#参数
#pageip-openIphuan()返回结果-小幻ip网页单页的全部IP代理 格式：list<dict> [{'httpX': 'httpX://ip:port'},{'httpX': 'httpX://ip:port'}]
#usenum-当可用ip总数等于usenum时，退出函数并返回可用ip列表 格式 int 建议为3
#返回 IP列表 格式：list<dict> [{'httpX': 'httpX://ip:port'},{'httpX': 'httpX://ip:port'}]

openurl = "https://forum.hkgolden.com/"
def okip(pageip,usenum):
    iplist = []
    while True:
        for i in pageip:

            #print(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
            #print(i)
            a = trydaili(openurl,i)
            if a != None:
                iplist.append(i)
                #print(a, "ok")
        if len(iplist) >= usenum:
            #print(iplist)
            return iplist
        else:
            iplist = openIpihuan(pn)[1]

pn = "?page=b97827cc"
iplist = []



def gd(number,ip):

    n = 1
    lastpage = []
    while True:
        print(n)
        headers = {
            'authority': 'api.hkgolden.com',
            'method': 'GET',
            'path': '/v1/view/{}/{}?sensormode=Y&hideblock=N'.format(number,n),
            'scheme': 'https',
            'accept': 'application/json, text/plain, */*',
            #'accept-encoding': 'gzip, deflate, br',
            'accept-language': 'zh-CN,zh;q=0.9',
            'hkgauth': 'null',
            'origin': 'https://forum.hkgolden.com',
            'referer': 'https://forum.hkgolden.com/',
            'sec-ch-ua': '"Chromium";v="92", " Not A;Brand";v="99", "Google Chrome";v="92"',
            'sec-ch-ua-mobile': '?0',
            'sec-fetch-dest': 'empty',
            'sec-fetch-mode': 'cors',
            'sec-fetch-site': 'same-site',
            'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36',
        }

        params = {
            'sensormode': 'Y',
            'hideblock': 'N'
        }

        url = "https://api.hkgolden.com/v1/view/{}/{}?".format(number,n)


        result = requests.get(url=url,headers=headers, params= params, proxies = ip, timeout = 15)

        #result.encoding = 'GBK'
        #result.encoding = 'utf-8'
        if result.status_code == 200:
            t = result.text
            zhutf8 = re.compile(r'[\u4e00-\u9fa5！？｡。＂＃＄％＆＇()（）＊＋，－／：；＜＝＞＠［＼］＾＿｀｛｜｝～｟｠｢｣､、〃《》「」『』【】〔〕〖〗〘〙〚〛〜〝〞〟〰〾〿–—‘\'‛“”„‟…‧﹏a-zA-Z0-9 ]+')

            zh = zhutf8.findall(t)

            zhutf = re.compile(r'[\u4e00-\u9fa5]+')
            zhu = zhutf.findall(t)

            if lastpage == zhu:
                break

            lastpage = zhu
            #idlist = re.findall('\"id\":(.*?),\"',t)
    #print(idlist)
            '''for id in idlist:
                print(id)
            #print(result.text)
            with open("adnum.txt","a",encoding="utf-8") as w:
                for i in idlist:
                    w.write(i)
                    w.write("")'''
            #print(t)
            with open("gaodeng6.txt","a",encoding="utf-8") as w:
                for z in zh:
                    zhutf = re.compile(r'[\u4e00-\u9fa5]')
                    zhu = zhutf.search(z)
                    if zhu:
                        w.write(z)
                        w.write("\n")

            n += 1
        else:
            break
#7451909

for i in range(7421225,1000000,-1):
    try:
        if len(iplist) < 1:
            pn = openIpihuan(pn)[0]
            ipl = openIpihuan(pn)[1]
            iplist = okip(ipl, 3)
        print(i)
        gd(i, iplist[-1])

    except Exception as e:
        print(e)
        iplist.pop()
        time.sleep(100)
